package com.example.vspm.service.impl;

import com.example.vspm.entity.Person;
import com.example.vspm.entity.Result;
import com.example.vspm.mapper.LoginMapper;
import com.example.vspm.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public Result login(Person person, HttpSession session) {
        Result res = new Result();
        res.setCode(-1);
        res.setDetail(null);
        try {
            Integer pid = loginMapper.loginPerson(person);
            if (pid == null) {
                res.setCode(-1);
                res.setMsg("用户名或密码错误");
            }else {
                person = loginMapper.findPerson(pid);
                res.setCode(200);
                res.setMsg("登录成功");
                res.setDetail(person);
                session.setAttribute("person",person);
                Object obj = session.getAttribute("person");
                Person per = (Person)obj;
                System.out.println(per.getPersonName());
            }
        }catch (Exception e){
            res.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
}
